package com.kotlin.android.ktx.ext

/**
 *
 * Created on 2020/5/13.
 *
 * @author o.s
 */

const val PROTOCOL_HTTPS = "https://"
const val PROTOCOL_HTTP = "http://"

fun String.protocolUrl(isHttps: Boolean = false): String {
    return if (isHttps) {
        when {
            startsWith(PROTOCOL_HTTPS, true) -> {
                this
            }
            startsWith(PROTOCOL_HTTP, true) -> {
                replaceFirst(PROTOCOL_HTTP, PROTOCOL_HTTPS, true)
            }
            else -> {
                "$PROTOCOL_HTTPS$this"
            }
        }
    } else {
        when {
            startsWith(PROTOCOL_HTTP, true) -> {
                this
            }
            startsWith(PROTOCOL_HTTPS, true) -> {
                replaceFirst(PROTOCOL_HTTPS, PROTOCOL_HTTP, true)
            }
            else -> {
                "$PROTOCOL_HTTP$this"
            }
        }
    }
}

/**
 * 检查非空
 */
fun <T> checkNotNullState(any: T?): T {
    if (any == null) {
        throw NullPointerException("$any is null")
    }
    return any
}